package com.air.designmodel.fifteen.proxy;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * jdk动态代理和静态代理测试
 *
 * @author shent
 */
public class Test {
    public static void main(String[] args) {
        Subject realSubject = new RealSubject();
        //静态代理
        Subject realSubjectProxy = new RealSubjectProxy(realSubject);
        //动态代理
        InvocationHandler handler = new DynamicProxy(realSubject);
        Subject subject = (Subject) Proxy.newProxyInstance(handler.getClass().getClassLoader(), realSubject.getClass().getInterfaces(), handler);

        System.out.println(subject.getClass().getName());
        System.out.println("///////////动态代理类 do rent///////////");
        subject.rent();
        System.out.println("///////////动态代理类 do hello///////////");
        subject.hello("world");

        System.out.println("///////////静态代理类 do rent///////////");
        realSubjectProxy.rent();
        System.out.println("///////////静态代理类 do hello///////////");
        realSubjectProxy.hello("world");

    }
}
